package com.example.memo1.util;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class DBUtil extends SQLiteOpenHelper {
    private static final String DATABASE_NAME="db_memo.db";//数据库名字

    public static SQLiteDatabase db=null;//用它来操作数据库

    private static final int DATABASE_VERSION=7;//每一次对数据库的更新都要对其加1


    public DBUtil( Context context) {//初始化创建数据库
        super(context, DATABASE_NAME, null, DATABASE_VERSION,null);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {//数据库初始化加载代码
        db.execSQL("PRAGMA foreign_keys=false");
        db.execSQL("drop table if exists d_admin");//如果表存在则删除
        db.execSQL("CREATE TABLE d_admin(s_id varchar(20) primary key," +
              "s_pwd varchar(20)," +
                "s_name varchar(20)," +
               "s_sex varchar(20)," +
                " s_phone varchar(20)," +
               " s_age varchar(20))");
db.execSQL("INSERT INTO d_admin VALUES('root','123456','凌','女','10234321','20')");

        //编号  标题  内容  时间

        db.execSQL("drop table if exists d_record");//如果表存在则删除
        db.execSQL("CREATE TABLE d_record(s_id varchar(50) primary key," +
               "s_title varchar(20)," +
             " s_con text," +
               "s_uid varchar(20),"+//用户id
             " s_time varchar(20))");
        db.execSQL("INSERT INTO d_record VALUES('1','记录一下今天生活','吃了好多零食','root','2023年12月6 AM12点06')");

       // db.execSQL("PRAGMA foreign_keys=true");

    }



    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {//数据库变动则执行
        onCreate(db);
    }
}
